package com.gitee.absolute_spirit.hiot_android.ui.home

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.gitee.absolute_spirit.hiot_android.data.source.network.DeviceNetworkRepository
import com.gitee.absolute_spirit.hiot_android.data.source.proto.DataStoreRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted.Companion.Lazily
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class HomeViewModel @Inject constructor(
    private val deviceNetworkRepository: DeviceNetworkRepository,
    private val dataStoreRepository: DataStoreRepository,
) : ViewModel() {

    private var _uiState = MutableStateFlow(listOf(HomeViewState()))

    //        var uiState: StateFlow<List<HomeViewState>> = _uiState.asStateFlow()
    var uiState = deviceNetworkRepository.getAllFlow.stateIn(
        scope = viewModelScope,
        started = Lazily,
        initialValue = listOf(HomeViewState())
    )


    init {
        viewModelScope.launch(Dispatchers.IO) {
//            _uiState.value = deviceNetworkRepository.getAll()
        }
    }

    fun updateNavigateArgs(id: Int, type: String) {
        viewModelScope.launch {
            dataStoreRepository.saveData(id, type)
        }
    }
}